pub use pest::Parser;
pub use pest_derive::Parser;
use std::fs;
use std::env;
use std::collections::HashMap;
use std::path::{Path, PathBuf};

#[derive(Parser)]
#[grammar = "ini.pest"]
pub struct INIParser;

pub fn test_ini() {

    let file_name = "config.ini"; 
    let mut file_path = env::current_dir().unwrap();
    file_path.push("src");
    file_path.push(file_name);

    let unparsed_file = fs::read_to_string(file_path).expect("cannot read file");

    let file = INIParser::parse(Rule::file, &unparsed_file)
        .expect("unsuccessful parse") // unwrap the parse result
        .next().unwrap(); // get and unwrap the `file` rule; never fails

    let mut properties: HashMap<&str, HashMap<&str, &str>> = HashMap::new();
    let mut cur_sec_nm = "";

    for line in file.into_inner() {
        match line.as_rule() {
            Rule::section => {
                let mut inner_rules = line.into_inner();
                cur_sec_nm = inner_rules.next().unwrap().as_str();
            }
            Rule::property => {
                let mut inner_rules = line.into_inner();
                let name = inner_rules.next().unwrap().as_str();
                let vaule = inner_rules.next().unwrap().as_str();
                let current_section = properties.entry(cur_sec_nm).or_default();
                current_section.insert(name, vaule);
            }
            Rule::EOI => (),
            _ => unreachable!(),
        }
    }
    println!("{:#?}", properties);
}